package com.example.exjnidemo.exbluetooth01.b;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

import com.example.exjnidemo.exbluetooth01.LogUtil;
import com.example.exjnidemo.exbluetooth01.i.ScanBluetoothListener;
import com.example.exjnidemo.exbluetooth01.m.YXJBluetoothManager;

public class ScanBluetoothReceiver extends BroadcastReceiver {
    /**
     * 匹配密钥
     */
    private String Pin = "0000" ;

    private ScanBluetoothListener scanBluetoothListener;

    public void setScanBluetoothListener(ScanBluetoothListener scanBluetoothListener) {
        this.scanBluetoothListener = scanBluetoothListener;
    }



    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        //发现设备
        BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);


        switch (action){

            /**
             * 开始扫描
             */
            case BluetoothAdapter.ACTION_DISCOVERY_STARTED:
                LogUtil.v( "开始扫描...");
                if (scanBluetoothListener!=null){
                    scanBluetoothListener.startScan();
                }
                break;

            /**
             * 结束扫描
             *
             */
            case BluetoothAdapter.ACTION_DISCOVERY_FINISHED:
                LogUtil.v( "结束扫描...");
                if (scanBluetoothListener!=null){
                    scanBluetoothListener.finishedScan();
                }
                break;

            /**
             * 扫描获取到的蓝牙设备结果
             */
            case BluetoothDevice.ACTION_FOUND:
                Log.d("", "找到了可用设备=");
                if (scanBluetoothListener!=null){
                    scanBluetoothListener.scanResult(device);
                }
                break;


            /**
             * 确认配对
             */
            case BluetoothDevice.ACTION_PAIRING_REQUEST:
                LogUtil.v("确认配对");
                Bundle extras = intent.getExtras();
                if (extras!=null){
                    Object pairKey = extras.get(BluetoothDevice.EXTRA_PAIRING_KEY);
                    LogUtil.v("pairKey-->"+String.valueOf(pairKey));

                    boolean b = YXJBluetoothManager.getInstance()
                            .bondConfirmation(this, device,pairKey.toString());
                    if (scanBluetoothListener!=null)
                        scanBluetoothListener.onConfirmationBond(device, true);
                }
                break;


            /**
             * 配对回调状态
             */
            case BluetoothDevice.ACTION_BOND_STATE_CHANGED:
                switch (device.getBondState()) {
                    case BluetoothDevice.BOND_NONE:
                        LogUtil.v("取消配对");
                        if (scanBluetoothListener!=null)
                            scanBluetoothListener.onBondNone(device);
                        break;
                    case BluetoothDevice.BOND_BONDING:
                        LogUtil.v("配对中");
                        if (scanBluetoothListener!=null)
                            scanBluetoothListener.onBonding(device);
                        break;

                    case BluetoothDevice.BOND_BONDED:
                        LogUtil.v("配对成功");
                        if (scanBluetoothListener!=null)
                            scanBluetoothListener.onBonded(device);
                        break;
                }
                break;


            default:{
                LogUtil.v( "结束扫描...");
                if (scanBluetoothListener!=null){
                    scanBluetoothListener.finishedScan();
                }
                break;
            }
        }
    }
}
